from typing import Optional, List

from sqlalchemy import CHAR, DateTime, Float, Index, Integer, String, Text
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
import datetime


class Base(DeclarativeBase):
    pass


class Group(Base):
    __tablename__ = '_group'
    __table_args__ = (
        Index('IDX_9411506168cdb4f4bef9f514b6', 'group_name', unique=True),
    )

    group_name: Mapped[str] = mapped_column(String(50))
    group_manager_id: Mapped[str] = mapped_column(CHAR(100))
    group_avatar: Mapped[str] = mapped_column(CHAR(255))
    group_reg_time: Mapped[datetime.datetime] = mapped_column(DateTime)
    group_type: Mapped[int] = mapped_column(Integer)
    group_id: Mapped[str] = mapped_column(String(36), primary_key=True)


class Dialog(Base):
    __tablename__ = 'dialog'

    dialog_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    dialog_from_id: Mapped[str] = mapped_column(CHAR(100))
    dialog_target_id: Mapped[str] = mapped_column(CHAR(100))
    dialog_last_time: Mapped[datetime.datetime] = mapped_column(DateTime)
    last_message_id: Mapped[Optional[int]] = mapped_column(Integer)
    def to_dict(self):
        return {
            "dialog_id": self.dialog_id,
            "dialog_from_id": self.dialog_from_id,
            "dialog_target_id": self.dialog_target_id,
            "dialog_last_time": self.dialog_last_time.isoformat(),
            "last_message_id": self.last_message_id,
        }

    @classmethod
    def to_dicts(cls, dialogs: List["Dialog"]) -> List[dict]:
        return [dialog.to_dict() for dialog in dialogs]



class Expression(Base):
    __tablename__ = 'expression'

    expression_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    expression_type: Mapped[int] = mapped_column(Integer)
    expression_url: Mapped[str] = mapped_column(CHAR(255))


class ExpressionCollection(Base):
    __tablename__ = 'expression_collection'

    expression_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    user_id: Mapped[str] = mapped_column(String(255), primary_key=True)


class Friendship(Base):
    __tablename__ = 'friendship'

    add_friend_time: Mapped[datetime.datetime] = mapped_column(DateTime)
    friendship_state: Mapped[int] = mapped_column(Integer)
    friendship_type: Mapped[int] = mapped_column(Integer)
    top_if: Mapped[int] = mapped_column(Integer)
    from_user_id: Mapped[str] = mapped_column(String(255), primary_key=True)
    to_user_id: Mapped[str] = mapped_column(String(255), primary_key=True)
    agree_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime)


class GroupRelationship(Base):
    __tablename__ = 'group_relationship'

    top_if: Mapped[int] = mapped_column(Integer)
    group_member_type: Mapped[str] = mapped_column(CHAR(10))
    user_id: Mapped[str] = mapped_column(String(255), primary_key=True)
    group_id: Mapped[str] = mapped_column(String(255), primary_key=True)


class Info(Base):
    __tablename__ = 'info'

    info_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    info_from_id: Mapped[str] = mapped_column(CHAR(100))
    info_to_id: Mapped[str] = mapped_column(CHAR(100))
    info_type: Mapped[int] = mapped_column(Integer)
    info_content: Mapped[str] = mapped_column(CHAR(255))
    info_content_type: Mapped[str] = mapped_column(CHAR(50))
    info_created_time: Mapped[datetime.datetime] = mapped_column(DateTime)
    info_state: Mapped[int] = mapped_column(Integer)


class Message(Base):
    __tablename__ = 'message'

    message_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    message_from_id: Mapped[str] = mapped_column(CHAR(100))
    message_to_id: Mapped[str] = mapped_column(CHAR(100))
    message_type: Mapped[int] = mapped_column(Integer)
    message_content: Mapped[str] = mapped_column(Text)
    message_content_type: Mapped[str] = mapped_column(CHAR(20))
    message_created_time: Mapped[datetime.datetime] = mapped_column(DateTime)
    message_state: Mapped[int] = mapped_column(Integer)


class PreferSetting(Base):
    __tablename__ = 'prefer_setting'

    prefer_setting_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    theme_id: Mapped[int] = mapped_column(Integer)
    tone_if: Mapped[int] = mapped_column(Integer)
    tone_id: Mapped[int] = mapped_column(Integer)
    desktop_reminder: Mapped[int] = mapped_column(Integer)


class RandomAvatars(Base):
    __tablename__ = 'random_avatars'

    random_avatar_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    random_avatar_url: Mapped[str] = mapped_column(CHAR(255))


class Socket(Base):
    __tablename__ = 'socket'
    __table_args__ = (
        Index('IDX_601d1a7ead8e3702b6ce332d1a', 'socket_id', unique=True),
    )

    _id: Mapped[int] = mapped_column(Integer, primary_key=True)
    socket_id: Mapped[Optional[str]] = mapped_column(CHAR(100))
    ip: Mapped[Optional[str]] = mapped_column(CHAR(30))


class Theme(Base):
    __tablename__ = 'theme'

    frosted_glass_effect: Mapped[int] = mapped_column(Integer)
    theme_background_url: Mapped[str] = mapped_column(CHAR(255))
    theme_color: Mapped[str] = mapped_column(String(10))
    theme_font_color: Mapped[str] = mapped_column(String(10))
    theme_type_id: Mapped[int] = mapped_column(Integer)
    theme_opacity: Mapped[float] = mapped_column(Float)
    theme_id: Mapped[int] = mapped_column(Integer, primary_key=True)


class ThemeType(Base):
    __tablename__ = 'theme_type'

    theme_type_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    theme_type_name: Mapped[str] = mapped_column(CHAR(20))


class Tone(Base):
    __tablename__ = 'tone'

    tone_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    tone_name: Mapped[str] = mapped_column(CHAR(20))
    tone_url: Mapped[str] = mapped_column(CHAR(255))